<?php

namespace app\common\model;

/**
 * [Recharge  充值表]
 * @author [C。]
 * @return html
 */
class Recharge extends Base
{
    protected $name = 'recharge';

    /**
     * 获取充值列表
     * @param array $param
     */
    public function getRechargeByCondition($map, $from = 0, $size = 5) {
        $order = ['id' => 'desc'];
        return $this->where($map)
            ->limit($from, $size)
            ->order($order)
            ->select();
    }
    /**
     * 搜索获取充值列表
     * @param array $param
     */
    public function getlist($map, $from = 0, $size = 5,$key='',$ishome = 0) {
        if ($key == 1){
            $join = [
                ['app_member am','am.id = r.memid'],
            ];
            if($ishome == 1){
                $field = 'r.id,am.username,am.img,r.paytime,r.amt,r.rmk';
            }else{
                $field = 'r.*,am.username,am.tel,am.sex';
            }
        }elseif($key == 2) {
            $join = [
                ['app_acts aa', 'aa.id = r.item_id'],
            ];
            $field = 'r.*,aa.title actname';
        }elseif($key == 3) {
            $join = [
                ['app_company ac', 'ac.id = r.comid'],
            ];
            $field = 'r.*,ac.comname';
        }
        $order = ['r.id' => 'desc'];
        return $this->field($field)
            ->alias('r')
            ->join($join)
            ->where($map)
            ->limit($from, $size)
            ->order($order)
            ->select();
    }
    /**
     * 搜索获取充值列表总数
     * @param array $param
     */
    public function getCount($map,$key='') {
        if ($key == 1){
            $join = [
                ['app_member am','am.id = r.memid'],
            ];
            $field = 'r.*,am.username,am.tel,am.sex';
        }elseif($key == 2) {
            $join = [
                ['app_acts aa', 'aa.id = r.item_id'],
            ];
            $field = 'r.*,aa.title actname';
        }elseif($key == 3) {
            $join = [
                ['app_company ac', 'ac.id = r.comid'],
            ];
            $field = 'r.*,ac.comname';
        }
        return $this->field($field)
            ->alias('r')
            ->join($join)
            ->where($map)
            ->count();
    }
    /**
     * 获取充值列表总数
     * @param array $param
     */
    public function getRechargeCount($map) {
        return $this->where($map)->count();
    }
    /**
     * 获取已经募捐金额
     * @param array $param
     */
    public function getAlreadyRaising($aid) {
        return $this->where(['item_id'=>$aid,'states'=>1])->sum('amt');
    }
}
